package com.sh.shanhuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sh.shanhuo.R;
import com.sh.shanhuo.bean.ToolBoxContentBean;
import com.sh.shanhuo.database.web.WebInfoDBUtils;
import com.sh.shanhuo.database.web.WebInfoEntity;

import java.util.List;

public class ToolBoxAdapter extends BaseQuickAdapter<ToolBoxContentBean.RowsBean, BaseViewHolder> {

    private final Context context;

    public ToolBoxAdapter(Context context, List<ToolBoxContentBean.RowsBean> filterNames) {
        super(R.layout.toolbox_view, filterNames);
        this.context = context;
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void convert(BaseViewHolder helper, ToolBoxContentBean.RowsBean item) {
        //ImageView already = helper.getView(R.id.already);
        ImageView boxLogo = helper.getView(R.id.boxLogo);
        ImageView addBox = helper.getView(R.id.addBox);
        TextView boxName = helper.getView(R.id.boxName);
        TextView boxContent = helper.getView(R.id.boxContent);
        boxName.setText(item.getWebName());
        boxContent.setText(item.getIntroduce());
        Glide.with(context)
                .load(item.getLogoUrl())
                .placeholder(R.drawable.icon_content)
                .error(R.drawable.icon_content)
                .into(boxLogo);
        WebInfoEntity webInfoEntity = WebInfoDBUtils.getWebInfoByWebId(context, item.getId());
        if (webInfoEntity != null && webInfoEntity.deleteFlag == 0) {
            addBox.setBackground(context.getResources().getDrawable(R.drawable.icon_added, null));
            addBox.setClickable(false);
        } else {
            addBox.setBackground(context.getResources().getDrawable(R.drawable.icon_add, null));
            addBox.setClickable(true);
        }
        addBox.setOnClickListener(v -> {
            WebInfoEntity webInfoEntity1 = new WebInfoEntity();
            webInfoEntity1.webId = item.getId();
            webInfoEntity1.description = item.getIntroduce();
            webInfoEntity1.sortName = "默认";
            webInfoEntity1.onTop = 0;
            webInfoEntity1.serverTop = item.getTop();
            webInfoEntity1.isInServer = 1;
            webInfoEntity1.fromBox = 1;
            webInfoEntity1.webLink = item.getWebUrl();
            webInfoEntity1.webName = item.getWebName();
            webInfoEntity1.icon = item.getLogoUrl();
            webInfoEntity1.updateTime = System.currentTimeMillis();
            WebInfoDBUtils.insertWebInfo(context, webInfoEntity1);
            addBox.setBackground(context.getResources().getDrawable(R.drawable.icon_added, null));
        });
    }
}
